package com.cn.lp.export.plugin.process;

import com.cn.lp.common.enums.EnumID;
import com.cn.lp.common.utils.BlankAide;
import com.cn.lp.export.formatter.TypeFormatter;
import com.cn.lp.export.loader.ClassFilter;
import com.cn.lp.export.loader.ClassFilterHelper;
import com.cn.lp.export.loader.ClassIncludeFilter;
import com.cn.lp.export.loader.ScanClassInfo;
import com.cn.lp.export.plugin.common.CommonExportAllProcess;
import com.cn.lp.export.plugin.common.CommonExportTask;
import com.cn.lp.export.plugin.entity.EnumEntity;
import com.cn.lp.export.plugin.entity.EnumValueEntity;
import com.cn.lp.export.plugin.entity.EnumValueFieldEntity;
import com.cn.lp.export.source.SourceEnumValue;
import com.cn.lp.export.source.SourceEnumValueField;
import com.cn.lp.export.source.SourceType;
import com.cn.lp.export.table.TableAttribute;
import org.apache.maven.plugins.annotations.Mojo;

import java.util.*;
import java.util.function.Predicate;

/** 单个输出plugin */
public class EnumExportAll extends CommonExportAllProcess {

    public EnumExportAll(CommonExportTask exportTask) {
        super(exportTask.getOutPutDirection(), exportTask.getSourcePath(), exportTask.getClassPaths(),
                exportTask.getBasePackages(), exportTask.getDependencyPaths(), exportTask.getMvlPath(),
                exportTask.getMvlText(), exportTask.isLogParam(), exportTask.getOutPutFileName(),
                exportTask.getLogLevel());
    }

    @Override
    protected TableAttribute createTableAttribute() {
        return new TableAttribute() {
            private EnumEntity entity;
            public void putAttribute(ScanClassInfo classInfo, TypeFormatter typeFormatter) {
                EnumEntity enumEntity = new EnumEntity();
                enumEntity.setName(classInfo.getSimpleName());
                enumEntity.setDesc(classInfo.getSourceClass().getDesc());
                enumEntity.setTypeName(classInfo.getClassName());
                List<EnumValueEntity> valueList = new ArrayList<>();
                List<SourceEnumValue> enumValueList = classInfo.getSourceClass().getEnumValueList();
                for (SourceEnumValue sourceEnumValue : enumValueList) {
                    EnumValueEntity enumValueEntity = new EnumValueEntity();
                    enumValueEntity.setName(sourceEnumValue.getName());
                    enumValueEntity.setDesc(sourceEnumValue.getDesc());
                    enumValueEntity.setOrdinal(sourceEnumValue.getOrdinal());
                    List<EnumValueFieldEntity> fieldList = new ArrayList<>();
                    if(BlankAide.isNotBlank(sourceEnumValue.getFieldList())) {
                        for (SourceEnumValueField enumValueField : sourceEnumValue.getFieldList()) {
                            EnumValueFieldEntity fieldEntity = new EnumValueFieldEntity();
                            fieldEntity.setName(enumValueField.getName());
                            fieldEntity.setValue(enumValueField.getValue());
                            fieldList.add(fieldEntity);
                        }
                    }
                    enumValueEntity.setFieldList(fieldList);
                    valueList.add(enumValueEntity);
                }
                enumEntity.setValueList(valueList);
                entity = enumEntity;
            }

            @Override
            public Map<String, Object> getAttribute() {
                Map<String, Object> map = new HashMap<>();
                map.put("entity", entity);
                return map;
            }

        };
    }

    @Override
    protected ClassFilter createClassFilter() {
        return ClassFilterHelper.ofInclude(new Predicate<ScanClassInfo>() {
            @Override
            public boolean test(ScanClassInfo scanClassInfo) {
                // 获取枚举
                return scanClassInfo.getSourceClass().getClassType().isEnum();
            }
        });
    }

    @Override
    public String getProcessName() {
        return "EnumExportAll";
    }
}

//把自定义插件绑定在项目的生命周期中
//phase标签里面定义要绑定的生命周期，id用于命令，可以自己定义。
/**
 * <plugin>
 *     <groupId>org.example</groupId>
 *     <artifactId>test-maven-plugin</artifactId>
 *     <version>1.0-SNAPSHOT</version>
 *     <executions>
 *         <execution>
 *             <id>test1</id>
 *             <phase>compile</phase>
 *             <goals>
 *                 <goal>sayhi</goal>
 *             </goals>
 *         </execution>
 *     </executions>
 * </plugin>
 */

// 高版本需要加上这个
//--add-opens java.base/java.lang=ALL-UNNAMED